<%@ page trimDirectiveWhitespaces="true" %>
<%@page import="java.net.URLDecoder"%>
<%@page import="com.alibaba.fastjson.JSON"%>
<%@page import="com.jason.imagesnode.MyStringUtil"%>
<%@page import="com.jason.imagesnode.QC"%>
<%@page import="org.json.JSONObject"%>
<%@page import="java.util.UUID"%>
<%@ page import="java.util.Date"%>
<%@ page import="java.text.SimpleDateFormat"%>

<%@ page import="java.util.HashMap"%>
<%@ page import="java.util.Map"%>
<%@ page import="com.google.zxing.common.BitMatrix"%>
<%@ page import="com.google.zxing.MultiFormatWriter"%>
<%@ page import="com.google.zxing.BarcodeFormat"%>
<%@ page import="com.google.zxing.EncodeHintType"%>
<%@ page import="com.jason.imagesnode.MatrixToImageWriter"%>

<%@ page import="java.util.Iterator"%>
<%@ page import="java.util.List"%>
<%@ page contentType="text/html; charset=UTF-8"%>

<%@ page import="sun.misc.BASE64Decoder"%>
<%@ page import="java.io.FileOutputStream"%>
<%@ page import="java.io.File"%>

<%
	String sId = UUID.randomUUID().toString();
	String fileName = "";
	
	response.setHeader("Access-Control-Allow-Origin","*");

	JSONObject obj = new JSONObject();
	obj.put("result", "Y");
	String filenames = "";
	boolean flag = true;
	
	try {
		request.setCharacterEncoding("UTF-8");
		
		String content = (String)request.getParameter("content");
		
		List<QC> list = JSON.parseArray(content, QC.class);
		
		String basePath = getServletContext().getRealPath("/");
	
		for(int i = 0; i < list.size(); ++i){
			QC qc = list.get(i);
			
			String filename = qc.getFilename();
			String filepath = MyStringUtil.getFilePath(filename);
			File f = new File(basePath + filepath);
			if (!f.exists()) {
				f.mkdirs();
			}
			
			String path = basePath + filepath+"/" + filename;// 文件绝对路径
			
			MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
		     
		    Map hints = new HashMap();
		    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		    BitMatrix bitMatrix = multiFormatWriter.encode(URLDecoder.decode(qc.getId(),"utf-8"), BarcodeFormat.QR_CODE, 400, 400,hints);
		    File file1 = new File(path);
		    MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file1);
			
			System.out.println(basePath + filepath+"/" + filename);
		}
	} catch (Exception e) {
		obj.put("errMsg", e);
		obj.put("result", "N");
		e.printStackTrace();
	} finally {
		try {
			
		} catch (Exception e) {
		}
		//out.println("Access-Control-Allow-Origin:*\r\n");
		out.println(obj.toString());
	}
%>
